package com.atguigu.gmall.common.util;

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.util.ClassUtils;

import java.io.IOException;


/**
 * @author gsy
 * @date 2021年08月25日23:15
 * Description
 */
public class FastDFSUtils {
    static {
        String path = ClassUtils.getDefaultClassLoader().getResource("fdfs_client.conf").getPath();
        try {
            //加载TrackerIp和port的配置文件
            ClientGlobal.init(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String uploadFile(byte[] file,String ext){
        String fileId = null;
        try {
            //跟踪器的客户端
            TrackerClient trackerClient = new TrackerClient();

            //获取存储节点的IP和port
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageClient1 storageClient1 = new StorageClient1(trackerServer,null);
            fileId = storageClient1.upload_file1(file,
                    ext, null);
            return fileId;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

